package com.example.service;

import com.example.bean.Result;
import com.example.bean.User;
import com.example.bean.UserVo;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    //根据用户名查询用户
    public User SelectUser(String name) {
        User user = userMapper.SelectUserByName(name);
        return user;
    }

    //创建新用户
    public int registerUser(int uid, String name, String sex, String phone, String password, String email) {
        int resultCount = userMapper.registerUser(uid, name, sex, phone, password, email);
        return resultCount;
    }

    public Result login(String username, String password) {
        User user = userMapper.SelectUser(username, password);
        if (user != null) {
            UserVo userVo= new UserVo();
            userVo.setUid(user.getUId());
            //这里token直接用一个uuid
            userVo.setToken(UUID.randomUUID().toString());
            userVo.setUser(user);
            return new Result(200, "", userVo);
        }
        return new Result(400, "登录失败", "");
    }
    //查找用户uid的最大值
    public int SelectMaxuId(){
         return userMapper.SelectMaxuId();
    }
}
